package test1;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.StringReader;

public class XPathExample {
    public static void main(String[] args) throws Exception {
        String xml = "<html><body><input type='text' name='visible' /><input type='hidden' name='hidden' value='111111111'/></body></html>";
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new InputSource(new StringReader(xml)));

        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();

        // XPath expression to select all 'input' elements with a 'hidden' attribute
        String expression = "//input[@type='hidden']";
        NodeList nodes = (NodeList) xpath.evaluate(expression, document, XPathConstants.NODESET);

        for (int i = 0; i < nodes.getLength(); i++) {
            System.out.println("Found hidden input: " + nodes.item(i).getAttributes().getNamedItem("name").getNodeValue());
        }
    }
}